// SPDX-License-Identifier: GPL-2.0-or-later

#include <Snake/Game.hpp>

#include <Snake/Position.hpp>
#include <Snake/Direction.hpp>

#include <Snake/Snake.hpp>

Game::Game(uint8_t size) : size(size)
{
	this->apple = {10, 10};
}


void Game::doStep()
{
	const Position nextPosition = this->snake.getNextPosition();

	if(this->isInsideField(nextPosition))
	{
		if(nextPosition == this->apple)
		{
			this->apple = {this->apple.x + 1, this->apple.y + 1};
			snake.grow();
		}
		else
		{
			this->snake.move();
		}
	}
	else
	{
		this->snake = Snake();
	}
}



bool Game::isInsideField(const Position& position) const
{
	if(position.x < 0 || position.x >= this->size)
	{
		return false;
	}
	else if(position.y < 0 || position.y >= this->size)
	{
		return false;
	}

	return true;
}
